package com.lk.base.utils;

import org.springframework.beans.BeanUtils;

import java.text.MessageFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public static boolean isEmpty(String str) {
		if(str == null || str.length() == 0) {
			return true;
		}
		return false;
	}
	public static boolean isNumericZidai(String str) {
		for (int i = 0; i < str.length(); i++) {
			System.out.println(str.charAt(i));
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	public static boolean isNumeric(String str){

		Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;


	}
	public static String toListStr(List<String> list,String separator) {
		StringBuffer sb = new StringBuffer();
		for (String string : list) {
			sb.append(string);
			sb.append(separator);
		}
		if(sb.length() > 0) {
			String substring = sb.substring(0, sb.length() - 1);
			return substring;
		}
		return sb.toString();
	}

	public static String toListInteger(List<Integer> list,String separator) {
		List<String> listnew = new ArrayList<String>();
		for (Integer integer : list) {
			listnew.add(integer.toString());
		}
		return toListStr(listnew, separator);
	}

	public static List<Integer> toIntegerList(String[] arr){
		List<Integer> listnew = new ArrayList<Integer>();
		for (String str : arr) {
			listnew.add(Integer.parseInt(str));
		}
		return listnew;
	}

	public static Integer []  toIntegerArr(String[] arr){
		Integer [] ints = new Integer[arr.length];
		for (int i = 0; i < arr.length; i++) {
			ints[i] = Integer.parseInt(arr[i]);
		}
		return ints;
	}

	public static <T> List<?> copyList(List<?> t1,Class clazz){
		List<Object> t2s = new ArrayList<>();
		try {
			for (Object object : t1) {
				Object obj = clazz.newInstance();
				BeanUtils.copyProperties(object, obj);
				t2s.add(obj);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return t2s;
	}


	public static String getMessage(String message, String params, Map<String, String> map) {
		String keys[] = params.split(",");
		String list[] = new String[keys.length];
		int i =  0;
		for(String key : keys){
			list[i] = map.get(key);
			i++;
		}

		return MessageFormat.format(message, list);
	}

	public static List<String> everyLenStrs(String data, int len,int totalLen){
		List<String> strs = new ArrayList<>();
		for(int i=0;i<totalLen;i+=len){
		    char[] a=new char[len];
		    for(int j=i;j<i+len;j++){
		    a[j-i]=data.charAt(j);
		    }
		    //这里可以用一个数组存储分割好的值
		    strs.add(String.valueOf(a));
		    System.out.println(String.valueOf(a));
		}
		return strs;

	}

	public static String substring(String str ,int start) {
		if(str.indexOf(" ")!=-1) {
			str = str.replaceAll(" ", "");
		}
		return substring(str, start, str.length());
	}
	public static String substring(String str ,int start , int end) {
		if(str.indexOf(" ")!=-1) {
			return str.replaceAll(" ", "").substring(start, end);
		}else {
			return str.substring(start, end);
		}
	}
	public static float toFloat(String str,int index){
		return Float.valueOf(new StringBuffer(str).reverse().insert(index, ".").reverse().toString());
	}
	public static String toFloatStr(String str,int index){
		return toFloat(str,index) + "";
	}

	public static String insert(String str, int num, String insertStr) {
		String regex = "(.{"+num+"})";
		str = str.replaceAll(regex, "$1" + insertStr);
		return str;
	}
	public static String reverse(String str,int num){

		StringBuffer stringBuffer = getReverse(str, num);
		return HexUtils.parseHex2Float(stringBuffer.toString())+"";
	}

	public static StringBuffer getReverse(String str, int num) {
		str =  insert(str, num, ",");
		List<String> list = Arrays.asList(str.split(","));
		Collections.reverse(list);
		StringBuffer stringBuffer = new StringBuffer();
		for (String s:list) {
			stringBuffer.append(s);
		}
		return stringBuffer;
	}
	//字符串左补0
	public static String addZeroForNum(String str, int length) {
		int strLen = str.length();
		if (strLen < length) {
			while (strLen < length) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(str);
				str = sb.toString();
				strLen++;
			}
		}
		return str;
	}
	//字符串右补自定义字符串
	public static String addParaForNum(String str, int length,String para) {
		int strLen = str.length();
		if (strLen < length) {
			while (strLen < length) {
				StringBuffer sb = new StringBuffer();
				sb.append(str).append(para);
				str = sb.toString();
				strLen++;
			}
		}
		return str;
	}
}
